package com.heimali.sf.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.heimali.sf.R;
import com.heimali.sf.SettingActivity;
import com.heimali.sf.config.CommonConfig;
import com.heimali.sf.config.Constants;

/**
 * 受理费
 */
public class RegFeeFragment extends BaseFragment {
	public static final String TAG = "RegFeeFragment";

	private EditText mInputEditText;
	private TextView mPropertyFeeText;
	private TextView mDivorceFeeText;
	private TextView mDamagesFeeText;
	private TextView mTipText;
	private RadioGroup mRebateRadioGroup;
	private Button mSettingButton;

	CommonConfig config = CommonConfig.getInstance();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_reg_fee, container, false);
		mPropertyFeeText = (TextView) v.findViewById(R.id.property_fee_amount);
		mDivorceFeeText = (TextView) v.findViewById(R.id.divorce_fee_amount);
		mDamagesFeeText = (TextView) v.findViewById(R.id.damages_fee_amount);
		initFeeText();

		mTipText = (TextView) v.findViewById(R.id.tip);

		mInputEditText = (EditText) v.findViewById(R.id.money_input);
		mInputEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				calcFee();
			}
		});

		mRebateRadioGroup = (RadioGroup) v.findViewById(R.id.rebate_group);
		initRebateGroup();

		mSettingButton = (Button) v.findViewById(R.id.setting_default_fee);
		mSettingButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				startActivity(new Intent(activity, SettingActivity.class));
			}
		});
		return v;
	}

	@Override
	public void onResume() {
		super.onResume();
		refreshUI();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return true;
		}
		return false;
	}

	public void refreshUI() {
		initFeeText();
		calcFee();
		mTipText.setText(getString(R.string.reg_fee_tip, String.valueOf(defaultPropertyFee),
				String.valueOf(defaultDivorceFee), String.valueOf(defaultDamagesFee)));
	}

	private void calcFee() {
		String inputStr = mInputEditText.getText().toString();
		double input = TextUtils.isEmpty(inputStr) ? 0 : Double.valueOf(inputStr);
		mPropertyFeeText.setText(String.valueOf(formatF2(calcProperty(input) * rebate)));
		mDivorceFeeText.setText(String.valueOf(formatF2(calcDivorce(input) * rebate)));
		mDamagesFeeText.setText(String.valueOf(formatF2(calcDamages(input) * rebate)));
	}

	private void initRebateGroup() {
		rebate = Double.valueOf(config.getConfig(Constants.SP_DEFAULT_REBATE, "1.0"));
		if (rebate == 1.0) {
			((RadioButton) mRebateRadioGroup.getChildAt(0)).setChecked(true);
			((RadioButton) mRebateRadioGroup.getChildAt(1)).setChecked(false);
		} else {
			((RadioButton) mRebateRadioGroup.getChildAt(0)).setChecked(false);
			((RadioButton) mRebateRadioGroup.getChildAt(1)).setChecked(true);
		}
		mRebateRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.rebate_full:
					rebate = 1.0;
					config.setConfig(Constants.SP_DEFAULT_REBATE, String.valueOf(rebate));
					calcFee();
					break;
				case R.id.rebate_half:
					rebate = 0.5;
					config.setConfig(Constants.SP_DEFAULT_REBATE, String.valueOf(rebate));
					calcFee();
					break;
				default:
					break;
				}
			}
		});
	}

	private void initFeeText() {
		defaultPropertyFee = Double.valueOf(config.getConfig(Constants.SP_DEFAULT_PROPERTY_FEE,
				String.valueOf(Constants.DEFAULT_PROPERTY_FEE)));
		defaultDivorceFee = Double.valueOf(config.getConfig(Constants.SP_DEFAULT_DIVORCE_FEE,
				String.valueOf(Constants.DEFAULT_DIVORCE_FEE)));
		defaultDamagesFee = Double.valueOf(config.getConfig(Constants.SP_DEFAULT_DAMAGES_FEE,
				String.valueOf(Constants.DEFAULT_DAMAGES_FEE)));
		mPropertyFeeText.setText(String.valueOf(defaultPropertyFee));
		mDivorceFeeText.setText(String.valueOf(defaultDivorceFee));
		mDamagesFeeText.setText(String.valueOf(defaultDamagesFee));
	}

	/**
	 * 离婚案件
	 * 
	 * @param sum
	 * @return
	 */
	private double calcDivorce(double sum) {
		if (sum <= 200000) {
			return defaultDivorceFee;
		} else {
			return defaultDivorceFee + (sum - 200000) * 0.5 / 100;
		}
	}

	/**
	 * 人格权侵权损害赔偿
	 * 
	 * @param sum
	 * @return
	 */
	private double calcDamages(double sum) {
		if (sum <= 50000) {
			return defaultDamagesFee;
		} else if (sum <= 100000) {
			return defaultDamagesFee + (sum - 50000) * 1 / 100;
		} else {
			return defaultDamagesFee + (100000 - 50000) * 1 / 100 + (sum - 100000) * 0.5 / 100;
		}
	}

}
